//
//  22.从上往下打印二叉树.swift
//  数据结构与算法
//
//  Created by ZERO on 2021/5/19.
//

import Foundation
/*
 题目：从上往下打印出二叉树的每个节点，同层节点从左至右打印。
 思路：bfs 队列
 */
func offer_22() {
    /*
     https://www.nowcoder.com/practice/7fe2212963db4790b57431d9ed259701?tpId=13&tags=&title=&diffculty=0&judgeStatus=0&rp=1&tab=answerKey
     */
    print("测试用例查看剑指offer")
}

fileprivate class Solution {
    func printFromTopToBottom(_ root: BinaryTreeNode<Int>?) -> [Int] {
        // write code here
        guard root != nil else { return [] }
        var queue = [BinaryTreeNode<Int>]()
        queue.append(root!)
        var printArr = [Int]()
        while !queue.isEmpty {
            let node = queue.removeFirst()
            printArr.append(node.data)
            if let left = node.leftNode {
                queue.append(left)
            }
            if let right = node.rightNode {
                queue.append(right)
            }
        }
        return printArr
    }
}
